package com.brave.config;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import org.springframework.stereotype.Component;

/**
 * Created by Administrator on 2019/1/10.
 */
@Component
public class AliSMSConfiguration
{
    private static IAcsClient iAcsClient;

    public SendSmsResponse sendSms(String mobile, String verifyCode) throws ClientException
    {
        SendSmsRequest request = new SendSmsRequest();
        request.setPhoneNumbers(mobile);
        request.setSignName("有闲");
        request.setTemplateCode("SMS_172800181");
        JSONObject json = new JSONObject();
        json.put("code", verifyCode);
        request.setTemplateParam(json.toString());
        IAcsClient iAcsClient = iAcsClient();
        SendSmsResponse sendSmsResponse = (SendSmsResponse) iAcsClient.getAcsResponse(request);
        return sendSmsResponse;
    }
    
    public static IAcsClient iAcsClient() {
        try {
            if(null == iAcsClient) {
                IClientProfile profile = DefaultProfile.getProfile(
                        "cn-hangzhou", 
                        "LTAIXdbhwUzYPmQ0", 
                        "pe0tQzNyAwkAiyV1aBwNLPeAoHVXjW");
                DefaultProfile.addEndpoint(
                        "cn-hangzhou",
                        "cn-hangzhou", 
                        "Dysmsapi", 
                        "dysmsapi.aliyuncs.com");
                iAcsClient = new DefaultAcsClient(profile);
            }
        } catch (Exception var1) {
            var1.printStackTrace();
        }
        return iAcsClient;
    }
}